//import java.utils.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Diagram extends Panel{ 
	Frame f;
	TextField tfNoten = new TextField("0.0,0.0                                   ");
	Button bOK = new Button("OK");
	List<Double> noten = new ArrayList<Double>();
	
 	private class SetNoten implements ActionListener{
		public void actionPerformed(ActionEvent ae){
			noten = new ArrayList<Double>();
			StringTokenizer st = new StringTokenizer(tfNoten.getText(), ";|,| |#|");
			String note;
			while(st.hasMoreTokens()){
				note=st.nextToken();
				try{ 
					noten.add(Double.parseDouble(note));					
				}catch(Exception ex){
					System.out.println("Fehler bei Stringteil: " + note);
					System.out.println(ex.getMessage());
				}
			}
			repaint();
		}
		
 		public void test(){
 			System.out.println("Innerclass");
 		}
	}
	public void test(){
			System.out.println("Baseclass");
		}
	Diagram(){
			f = new Frame("Notengrafik");
			f.add(this);
			f.setSize( 900, 300 );
			f.addWindowListener( new WindowAdapter() {
				@Override
				public void windowClosing ( WindowEvent e ) { System.exit( 0 ); }
			});
			f.setLayout(new BorderLayout());
			f.add(this, BorderLayout.CENTER);
			setBackground(Color.WHITE);
			
			Panel pNorth = new Panel();
			pNorth.setLayout(new FlowLayout());
			pNorth.add(tfNoten);
			pNorth.add(bOK);
			bOK.setBackground(Color.WHITE);
			bOK.addActionListener(new SetNoten());
			pNorth.setBackground(Color.BLACK);
			f.add(pNorth, BorderLayout.NORTH);
			f.setVisible(true);
			tfNoten.selectAll();
		}
	
	
	
	public void paint(Graphics g){
		//koordinatenursprung
		double edgeDist=0.1; // abstand zum rand z.B. 0.1 fuer 10%
		Point origin = new Point((int)(edgeDist*getWidth()), (int)((1-edgeDist)*getHeight()));
		int maxAbscissa = (int)((1-edgeDist)*getWidth()); // rechtester wert auf xAchse
		int minAbscissa = (int)(edgeDist*getWidth()); // rechtester wert auf xAchse
		int maxOrdinate = (int)((edgeDist)*getHeight()); // hoechster wert auf yAchse
		int minOrdinate = (int)((1-edgeDist)*getHeight());
		int width = maxAbscissa - minAbscissa; // laenge der x-Achse
		int height = minOrdinate - maxOrdinate; // laenge y-achse
		
		
		// Y-Achse zeichnen
		g.drawLine(origin.x, origin.y, maxAbscissa, minOrdinate);
		// X-Achse zeichnen
		g.drawLine(origin.x, origin.y, (int)minAbscissa, (int)maxOrdinate);
		
		// noten auf y-achse eintragen
		double maxNote = 5.0;
		for(double i=1.0; i<=maxNote; i++){
			//System.out.println(origin.x+","+ (int)(origin.y-i/5*height)+","+ (origin.x-7) +","+(int)(origin.y-i/5*height));
			g.drawLine(origin.x, (int)(origin.y-i/maxNote*height), (origin.x-7), (int)(origin.y-i/maxNote*height));
			g.drawString(Double.toString(i),origin.x-25, (int)(origin.y-i/5*height));
		}
		
		// erster punkt
		Point lastPoint = null;
		if(noten.size() > 0)
			lastPoint = new Point((int)(origin.x+(double)1/noten.size()*width), (int)(origin.y-(height/maxNote*noten.get(0))));
		// x achse beschriften + linie zeichnen
		for(int i=1; i<=noten.size(); i++){
			int x = (int)(origin.x+(double)i/noten.size()*width); // x abstand
			// beschriften der X-Achse
			g.drawLine(x, origin.y, x, origin.y+7);
			g.drawString(Integer.toString(i)+". Semester",x-40, origin.y+20);
			
			// linie zeichnen
			if(noten.size() > 0){
				int newY =(int)(origin.y-(height/maxNote*noten.get(i-1)));
				if( newY < lastPoint.y ){
					g.setColor(Color.RED);
				}else if( newY > lastPoint.y){
					g.setColor(Color.GREEN);
				}
				g.drawLine(lastPoint.x, lastPoint.y, x, newY);
				g.setColor(Color.BLACK);
				lastPoint.x=x;
				lastPoint.y=newY;
			}
		}
	}
		
	public static void main (String args[]){
		new Diagram();
	}
}